package com.wangyb.wework.controller;

/**
 * @Author: wangyb
 * @Date: 2021/7/27 10:20 下午
 */

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.wangyb.wework.bo.WwMediumAddBo;
import com.wangyb.wework.bo.WwMediumEditBo;
import com.wangyb.wework.bo.WwMediumQueryBo;
import com.wangyb.wework.service.medium.IWwMediumService;
import com.wangyb.wework.utils.ali.AliyunOssUtils;
import com.wangyb.wework.vo.WwMediumVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 素材库 Controller
 *
 * @author wangyb
 * @date 2021-07-27
 */
@Api(value = "素材库 控制器", tags = {"素材库 管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wework/medium")
public class WwMediumController extends BaseController {

	private final IWwMediumService iWwMediumService;

	/**
	 * 查询素材库 列表
	 */
	@ApiOperation("查询素材库 列表")
	@PreAuthorize("@ss.hasPermi('wework:medium:list')")
	@GetMapping("/list")
	public TableDataInfo<WwMediumVo> list(@Validated WwMediumQueryBo bo) {
		TableDataInfo<WwMediumVo> tableDataInfo = iWwMediumService.queryPageList(bo);
		tableDataInfo.getRows().stream().forEach(wwMediumVo -> {
			String content = wwMediumVo.getContent();
			JSONObject json = JSONObject.parseObject(content);
			if (json.containsKey("imgPath")){
				String imgPath = json.getString("imgPath");
				String imgUrl = AliyunOssUtils.getUrl(imgPath);
				json.put("imgUrl",imgUrl);
				wwMediumVo.setContent(json.toJSONString());
			}
		});
		return tableDataInfo;
	}

	/**
	 * 导出素材库 列表
	 */
	@ApiOperation("导出素材库 列表")
	@PreAuthorize("@ss.hasPermi('wework:medium:export')")
	@Log(title = "素材库 ", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult<WwMediumVo> export(@Validated WwMediumQueryBo bo) {
		List<WwMediumVo> list = iWwMediumService.queryList(bo);
		ExcelUtil<WwMediumVo> util = new ExcelUtil<WwMediumVo>(WwMediumVo.class);
		return util.exportExcel(list, "素材库 ");
	}

	/**
	 * 获取素材库 详细信息
	 */
	@ApiOperation("获取素材库 详细信息")
	@PreAuthorize("@ss.hasPermi('wework:medium:query')")
	@GetMapping("/{id}")
	public AjaxResult<WwMediumVo> getInfo(@NotNull(message = "主键不能为空")
										  @PathVariable("id") Long id) {
		return AjaxResult.success(iWwMediumService.queryById(id));
	}

	/**
	 * 新增素材库
	 */
	@ApiOperation("新增素材库 ")
	@PreAuthorize("@ss.hasPermi('wework:medium:add')")
	@Log(title = "素材库 ", businessType = BusinessType.INSERT)
	@RepeatSubmit
	@PostMapping()
	public AjaxResult<Void> add(@Validated @RequestBody WwMediumAddBo bo) {
		return toAjax(iWwMediumService.insertByAddBo(bo) ? 1 : 0);
	}

	/**
	 * 修改素材库
	 */
	@ApiOperation("修改素材库 ")
	@PreAuthorize("@ss.hasPermi('wework:medium:edit')")
	@Log(title = "素材库 ", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping()
	public AjaxResult<Void> edit(@Validated @RequestBody WwMediumEditBo bo) {
		return toAjax(iWwMediumService.updateByEditBo(bo) ? 1 : 0);
	}
	/**
	 * 删除素材库
	 */
	@ApiOperation("删除素材库 ")
	@PreAuthorize("@ss.hasPermi('wework:medium:remove')")
	@Log(title = "素材库 " , businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
								   @PathVariable Long[] ids) {
		return toAjax(iWwMediumService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
	}
}
